package com.goldcn.picture.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.goldcn.common.CommonTools;
import com.goldcn.common.util.CommonUtils;
import com.goldcn.common.util.IdGen;
import com.goldcn.enumeration.ErrorCodeEnum;
import com.goldcn.goods.dto.PageInfo;
import com.goldcn.picture.dao.PicSpecialMapper;
import com.goldcn.picture.dao.PicStorageDetailMapper;
import com.goldcn.picture.dao.PicStorageMapper;
import com.goldcn.picture.dto.PicStorageDto;
import com.goldcn.picture.model.PicSpecial;
import com.goldcn.picture.model.PicStorage;
import com.goldcn.picture.model.PicStorageDetail;
import com.goldcn.picture.vo.PicAuditVo;
import com.goldcn.picture.vo.PicSpecialVo;
import com.goldcn.picture.vo.PicStorageVo;
import com.goldcn.picture.vo.StorageByUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
@Transactional
public class PicStorageService {

    @Autowired
    PicStorageMapper picStorageMapper;

    @Autowired
    PicStorageDetailMapper picStorageDetailMapper;

    /**
     * 添加出入库
     * @param picStorageVo
     * @return
     */
    public Integer addPicStorage(PicStorageVo picStorageVo){
        PicStorage picStorage=new PicStorage();
        picStorage.setStorageid(IdGen.get().nextId());
        picStorage.setAddtime(CommonUtils.getCurTimestamp());
        picStorage.setAuditor(picStorageVo.getUserid()!=null?picStorageVo.getUserid().toString():"");
        picStorage.setClassify(picStorageVo.getClassify());
        picStorage.setPattern(picStorageVo.getPattern());
        picStorage.setStatus(BigDecimal.ZERO.intValue());
        picStorage.setPurpose(picStorageVo.getPurpose());
        picStorage.setRemark(picStorageVo.getRemark());
        picStorage.setUserid(picStorageVo.getUserid());
        picStorage.setStoragetype(picStorageVo.getStoragetype());

        Integer result =  picStorageMapper.insertSelective(picStorage);
        if(result != 1){
            throw CommonTools.createException(ErrorCodeEnum.addSpecialFailure);
        }

        //添加出入库详细表
        if(picStorageVo.getPicids().size()>0){
            for (String pid:picStorageVo.getPicids()){
                PicStorageDetail detail=new PicStorageDetail();
                detail.setAddtime(CommonUtils.getCurTimestamp());
                detail.setStorageid(picStorage.getStorageid());
                detail.setSlaveid(IdGen.get().nextId());
                detail.setPictureid(Long.parseLong(pid));
                picStorageDetailMapper.insertSelective(detail);
            }
        }

        return result;
    }


    /**
     * 更新出入库
     * @param picStorageVo
     * @return
     */
    public Integer updateByPicStorage(PicStorageVo picStorageVo){

        PicStorage picStorage=new PicStorage();
        BeanUtils.copyProperties(picStorageVo,picStorage);

        Integer result =  picStorageMapper.updateByPrimaryKeySelective(picStorage);
        if(result != 1){
            throw CommonTools.createException(ErrorCodeEnum.updateSpecialFailure);
        }

        //添加出入库详细表
        if(picStorageVo.getPicids().size()>0){

            picStorageDetailMapper.deleteByStorageID(picStorage.getStorageid()) ;

            for (String pid:picStorageVo.getPicids()){
                PicStorageDetail detail=new PicStorageDetail();
                detail.setAddtime(CommonUtils.getCurTimestamp());
                detail.setStorageid(picStorage.getStorageid());
                detail.setSlaveid(IdGen.get().nextId());
                detail.setPictureid(Long.parseLong(pid));
                picStorageDetailMapper.insertSelective(detail);
            }
        }
        return result;
    }


    /**
     * 通过ID删除指定的出入库
     * @param storageID
     * @return
     */
    public Integer deleteByPrimaryKey(Long storageID){
        //判断标签是否使用
/*        if(picLabelMapper.labelUseCount(labelID)>0){
            throw CommonTools.createException(ErrorCodeEnum.lableIsUsed);
        }*/
        Integer result = picStorageMapper.deleteByPrimaryKey(storageID);
        if(result!=1){
            //抛出异常
            throw CommonTools.createException(ErrorCodeEnum.deleteSpecialFailure);
        }
        //删除所选图片
        picStorageDetailMapper.deleteByStorageID(storageID);
        return result;
    }

    /**
     * 根据ID查询出入库
     * @param specialID
     * @return
     */
    public PicStorageDto selectByPrimaryKey(Long specialID){

        PicStorageDto picStorageDto=new PicStorageDto();
        PicStorage picStorage=picStorageMapper.selectByPrimaryKey(specialID);
        BeanUtils.copyProperties(picStorage,picStorageDto);
        picStorageDto.setPicList(picStorageDetailMapper.selectPicByStorageId(specialID));

        return picStorageDto;
    }



    /**
     * 根据用户ID查询出入库
     * @param vo
     * @return
     */
    public Object selectListByUserId(StorageByUserVo vo,Integer pageNum, Integer pageSize){

        String orderBy = "addTime";
        String orderType = "desc";
        String orderStr = orderBy + " " + orderType;
        PageHelper.startPage(pageNum, pageSize,orderStr);//分页初始化
        StorageByUserVo storageByUserVo=new StorageByUserVo();
        storageByUserVo.setOpertype(vo.getOpertype());
        storageByUserVo.setUserid(vo.getUserid());
        Page page = (Page) picStorageMapper.selectListByUserId(storageByUserVo);
        return new PageInfo(page, page.getPages(), page.getTotal());

    }

    /**
     * 审核出入库
     * @param vo
     * @return
     */
    public Object auditStorage(PicAuditVo vo){
        PicStorage picStorage=picStorageMapper.selectByPrimaryKey(vo.getOperatorid());
        if (picStorage.getStatus()>0){
            throw CommonTools.createException(ErrorCodeEnum.auditStorageFailure);
        }
        picStorage.setOther1(vo.getAuditdes());
        picStorage.setStatus(vo.getStatus());
        int result=picStorageMapper.updateByPrimaryKeySelective(picStorage);
        return result;
    }







}
